package com.medium.house.typehandler;

import com.medium.house.enums.HouseTypeEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HouseTypeEnumHandler extends BaseTypeHandler<HouseTypeEnum> {
	
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, HouseTypeEnum parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setInt(i, parameter.getCode());
	}
	
	@Override
	public HouseTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
		int val = rs.getInt(columnName);
		return HouseTypeEnum.toEnum(val);
	}
	
	@Override
	public HouseTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		int val = rs.getInt(columnIndex);
		return HouseTypeEnum.toEnum(val);
	}
	
	@Override
	public HouseTypeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		int val = cs.getInt(columnIndex);
		return HouseTypeEnum.toEnum(val);
	}
}
